package com.cnfangmao.masterdata.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.BasicDataRuleDao;
import com.cnfangmao.masterdata.dto.BasicDataRuleDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataRule;
import com.cnfangmao.masterdata.service.BasicDataRuleService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 主数据字段控制规则定义 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class BasicDataRuleServiceImpl extends ServiceImpl<BasicDataRuleDao, BasicDataRule> implements BasicDataRuleService {

	/* (non-Javadoc)
	 * @see com.cnfangmao.masterdata.service.BasicDataRuleService#getBasicDateRuleByRuleName(java.lang.String)
	 */
	@Override
	public BasicDataRule getBasicDateRuleByRuleName(String ruleName) {
		QueryWrapper<BasicDataRule> queryWrapper = new QueryWrapper<BasicDataRule>();
		queryWrapper.eq("rule_name", ruleName);
		
		return getOne(queryWrapper);
	}

	/* (non-Javadoc)
	 * @see com.cnfangmao.masterdata.service.BasicDataRuleService#getBasicDataDependationsByPage(com.cnfangmao.masterdata.dto.GeneralSearchDto)
	 */
	@Override
	public GeneralPageVo<BasicDataRule> getBasicDateRulesByPage(GeneralSearchDto<BasicDataRuleDto> searchDto) {
		IPage<BasicDataRule> page = new Page<BasicDataRule>(searchDto.getPageNo(), searchDto.getPageSize());
		QueryWrapper<BasicDataRule> queryWrapper = new QueryWrapper<BasicDataRule>();
		queryWrapper.like(!StringUtils.isEmpty(searchDto.getConditions().getRuleName()), "RULE_NAME", searchDto.getConditions().getRuleName());
		queryWrapper.orderByDesc("MODIFIED_ON");
		
		IPage<BasicDataRule> pageRS = page(page, queryWrapper);
		
		GeneralPageVo<BasicDataRule> pageVo = new GeneralPageVo<BasicDataRule>();
		pageVo.setPageNo(pageRS.getCurrent());
		pageVo.setPageSize(pageRS.getSize());
		pageVo.setTotalCount(pageRS.getTotal());
		pageVo.setRecords(pageRS.getRecords());
		
		return pageVo;
	}

	
}
